import * as API from '../../../api/contactsApi';
import * as util from '../../../utils/util';

let app = getApp ();
Page ({
  /**
   * 页面的初始数据
   */
  data: {
    createBy: '',
    createDate: '',
    updateDate: '',
    updateBy: '',
    id: '',
    customerId: '',
    telephone: '',
    customName: '', //客户公司名称
    customId: '', //客户公司ID
    name: '', //姓名
    sex: '', //称呼
    email: '', //邮箱
    dept: '', //部门
    position: '', //职务
    createWay: '', //来源
    orderNum: '', //关注订单数
    isEdit: false,
    isAdmin: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let id = options.id || 'id';
    this.setData ({
      id: id,
    });
    this.getUserInfo ();
    API.contactsDetail (id).then (
      data => {
        let dataSource = data.customAccount;
        this.setData ({
          createBy: dataSource.createBy || '',
          createDate: dataSource.createDate || '',
          updateDate: dataSource.updateDate || '',
          updateBy: dataSource.updateBy || '',
          id: options.id || '',
          telephone: dataSource.telephone || '',
          customName: dataSource.customName || '', //客户公司名称
          customId: dataSource.customId || '', //客户公司ID
          name: dataSource.name || '', //姓名
          sex: dataSource.sex || '', //称呼
          email: dataSource.email || '', //邮箱
          dept: dataSource.dept || '', //部门
          position: dataSource.position || '', //职务
          createWay: dataSource.createWay || '', //来源
          orderNum: dataSource.orderNum || '0', //关注订单数
        });
      },
      err => {
        wx.showToast ({
          title: err,
          icon: 'none',
        });
      }
    );
  },

  getUserInfo: function () {
    API.updateSwitchInfo ().then (res => {
      this.setData ({
        isAdmin: res.isAdmin,
      });
    });
  },

  //删除联系人
  removeContacts: function (options) {
    util.toast ('体验版不可用');
  },
  //前往关注订单列表
  gotoOrderList: function (options) {
    util.preventMoreClick (this, () => {
      var id = this.data.id;
      var customId = this.data.customId;
      wx.navigateTo ({
        url: '../attentionOrder/attentionOrder?contactsId=' +
          id +
          '&customId=' +
          customId,
      });
    });
  },

  //输入校验去空格处理
  verification: function (e) {
    var name = e.currentTarget.dataset.name;
    this.setData ({
      [name]: e.detail.value.replace (/\s+/g, ''),
      isEdit: true,
    });
  },

  //编辑姓名
  editNameInput: function (e) {
    var name = e.detail.value;
    var beforeName = this.data.name;
    if (beforeName != name) {
      this.setData ({
        isEdit: true,
        name: name,
      });
    }
  },
  //编辑职务
  editPositionInput: function (e) {
    var position = e.detail.value;
    var beforePosition = this.data.position;
    if (beforePosition != position) {
      this.setData ({
        isEdit: true,
        position: position,
      });
    }
  },
  //编辑邮箱
  editEmailInput: function (e) {
    var email = e.detail.value;
    var beforeEmail = this.data.email;
    if (beforeEmail != email) {
      this.setData ({
        isEdit: true,
        email: email,
      });
    }
  },

  //编辑部门
  editDeptInput: function (e) {
    var dept = e.detail.value;
    var beforeDept = this.data.dept;
    if (beforeDept != dept) {
      this.setData ({
        isEdit: true,
        dept: dept,
      });
    }
  },

  //打电话
  callUp: function () {
    wx.makePhoneCall ({
      phoneNumber: this.data.telephone,
    });
  },

  formatDate: function (timeStamp) {
    return yyMMddFormatWith (timeStamp);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function (options) {
    var isEdit = this.data.isEdit;

    if (isEdit && this.data.isAdmin) {
      wx.showToast ({
        title: '管理员无法编辑',
        icon: 'none',
      });
    } else if (isEdit && !this.data.isAdmin) {
      var email = this.data.email;
      if (email.length > 0 && !this.data.isAdmin) {
        if (!util.validators.email.test (email)) {
          wx.showToast ({
            title: '修改失败,请输入正确的邮箱!',
            icon: 'none',
          });
          isEdit = false;
          return;
        }
      }
      var postbody = {
        id: this.data.id,
        name: this.data.name,
        email: this.data.email,
        dept: this.data.dept,
        position: this.data.position,
      };
      API.updateContacts (postbody).then (
        res => {
          wx.showToast ({
            icon: 'success',
            title: '修改成功',
            duration: 2000,
          });
        },
        err => {
          wx.hideLoading ();
          wx.showToast ({
            icon: 'none',
            title: err || '修改失败',
          });
        }
      );
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
});
